// import {routes} from '@/router/homeChildren';

// function hasRoles(roles,route){
//    if(route.meta&&route.meta.roles){
//       return roles.some(item=>route.meta.roles.includes(item))
//    }else{
//        return true
//    }
// }

// function filterRoutes(roles){
//    let res = [];
//    routes.forEach(item=>{
//        if(!item.redirect){
//             if(hasRoles(roles,{...item})){
//                 res.push({...item})
//             }
//        }
//    })
//    return res
// }

// const state = {
//     routes:[],
// };
// const mutations = {
//     'SET_ROUTES'(state,routes){
//         state.routes = routes
//     }
// };

// const actions = {
//     generateRoutes({commit},roles){
//         return new Promise((resolve,reject)=>{
//             let accessedRoutes;
//             if(roles.includes('admin')){
//                 accessedRoutes = routes
//             }else{
//                 accessedRoutes = filterRoutes(roles)
//             }
//             commit('SET_ROUTES',accessedRoutes)
//             resolve(accessedRoutes)
//         })
//     }
// };

// export default {
//     namespaced:true,
//     state,
//     actions,
//     mutations
// }

import {routes} from '@/router/homeChildren';

function filterRoutes(roles){
    let res = [];
    routes.forEach(item=>{
        if( hasRoles({...item},roles) ){
            res.push({...item})
        }
    })
    return res
}

function hasRoles(route,roles){
    if(route.meta.roles){
        return roles.some(role=>route.meta.roles.includes(role))
    }else{
        return true
    }
}


const state = {
    routes:[],//路由
};
const mutations = {
    'SET_ROUTES'(state,routes){
        state.routes = routes
    }
};
const actions = {
    setRoutes({commit},roles){
        return new Promise((resole,reject)=>{
            let assetsRoutes = [];
            if(roles.includes('admin')){
                assetsRoutes = routes
            }else{
                assetsRoutes = filterRoutes(roles)
            }
            commit('SET_ROUTES',assetsRoutes)
            resole(assetsRoutes)
        })
    }
};


export default {
    namespaced:true,
    state,
    mutations,
    actions
}